﻿using System;
using System.Data;
using System.Web.UI;
using Baimasoft.FrameWork.Base;
using Baimasoft.Manage.SqlServerDAL;
using HsTec.Mobile.Green.BLL;

public partial class Logon : System.Web.UI.Page
{
    BaimasoftFile MyFile = new BaimasoftFile();
    CommClass MyCl = new CommClass();
    UserManage MyUM = new UserManage();
    LogManage MyLog = new LogManage();
    BenchmarkBLL bll = new BenchmarkBLL();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            LogonID.Focus();
        }
    }

    protected void Submit_ServerClick(object sender, EventArgs e)
    {
        int iback = NetCommClass.JudgeAccreditInfo();
        if (iback == 1)
        {
            string sLogonID = MyCl.CutLimitString(LogonID.Text.ToString().Trim());
            string sPassword = MyCl.CutLimitString(Password.Text.ToString().Trim());
            if (sLogonID == "" && sPassword == "")
            {
                LogonID.Focus();
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('帐号和密码均不能为空!');</script>");
                return;
            }
            if (sLogonID == "")
            {
                LogonID.Focus();
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('帐号不能为空!');</script>");
                return;
            }
            if (sPassword == "")
            {
                Password.Focus();
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('密码不能为空!');</script>");
                return;
            }


            string sPsdMd5 = MyFile.Md5(sPassword);
            int back = MyUM.UserLogon(sLogonID, sPsdMd5).Rows.Count;
            if (back == 1)
            {
                //string url = Request.Url.Host.ToLower();
                //string sVisitIP = NetCommClass.GetIPByDomain(url);
                //long lIP = NetCommClass.ConvertIPToLong(sVisitIP);
                //int iIfIn = MyUM.JudgeIPIfIn(lIP); //当前IP是否是内网IP
                //if (iIfIn == 0) //是外网,需要手机验证码
                //{
                //    string sRndCode = NetCommClass.GetRandomCode(6);             //系统自动生成指定数目的数字
                //    int iBackCode = MyUM.MakerMobileCode(sLogonID, sRndCode);    //临时插入以指定帐户中,当验证结束后,即时空间验证码
                //    string sMobile = MyUM.GetMobile(sLogonID);
                //    //发送手机验证码
                //    ShortMessge MySM = new ShortMessge();
                //    int ibackSM = MySM.SendOneSM("0000", "jf", "106575290413", "MAH0010502", "10.254.231.147", "sa", "infox1eies2sps3was4!", sMobile, sRndCode, sLogonID);
                //    Response.Redirect("MobileCode.aspx?LogonID=" + sLogonID);  // + "&RndCode=" + sRndCode  
                //}
                //else
                //{
                DataTable UserInfo = new DataTable();

                UserInfo = MyUM.GetLogonUserInfo(sLogonID);
                if (UserInfo.Rows.Count == 0)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('登录失败,请联系管理员!');</script>");
                    return;
                }
                Session["Baimasoft_User_LogonID"] = UserInfo.Rows[0][1].ToString().Trim();         //帐户
                Session["Baimasoft_User_Alias"] = UserInfo.Rows[0][2].ToString().Trim();         //别名 
                Session["Baimasoft_Pers_ID"] = UserInfo.Rows[0][3].ToString().Trim();       //人员ID

                Session["Baimasoft_Role_Code"] = UserInfo.Rows[0][4].ToString().Trim();         //功能角色ID
                Session["Baimasoft_DbRole_Code"] = UserInfo.Rows[0][36].ToString().Trim();         //数据角色ID

                Session["Baimasoft_First_Page"] = UserInfo.Rows[0][5].ToString().Trim();         //首页
                Session["Baimasoft_User_BussiID"] = UserInfo.Rows[0][6].ToString().Trim();         //销售网点


                Session["Baimasoft_User_Effective"] = UserInfo.Rows[0][8].ToString().Trim();    //用户帐户是否有效
                Session["Baimasoft_Pers_Code"] = UserInfo.Rows[0][9].ToString().Trim();         //员工号

                Session["Baimasoft_Pers_Name"] = UserInfo.Rows[0][10].ToString().Trim();        //姓名
                Session["Baimasoft_Dept_ID"] = UserInfo.Rows[0][15].ToString().Trim();        //部门ID 
                Session["Baimasoft_Dept_Name"] = UserInfo.Rows[0][21].ToString().Trim();        //部门名称

                Session["Baimasoft_Role_Name"] = UserInfo.Rows[0][31].ToString().Trim();        //功能角色名称
                Session["Baimasoft_DbRole_Name"] = UserInfo.Rows[0][37].ToString().Trim();        //数据角色名称

                Session["Baimasoft_Report"] = bll.GetReportAreaID(UserInfo.Rows[0][0].ToString().Trim());

                string sPerID = (string)Session["Baimasoft_Pers_ID"];
                string sContent = "用户" + (string)Session["Baimasoft_User_LogonID"] + "登录系统成功！";
                string sIP = Page.Request.UserHostAddress;
                MyLog.InsertLog("1", sPerID, sContent, sIP);
                //月度报表提交提醒
                //string dayNow = DateTime.Now.Day.ToString();
                //string dayNews = SQLHelper.ExecuteString("select top 1 CValue from t_param where PersID='22' and Name='短信提醒时间'");
                //string dayMessage = SQLHelper.ExecuteString("select top 1 CValue from t_param where PersID='22' and Name='短信提醒信息'");
                //string news = SQLHelper.ExecuteString("select count(*) from tb_Message where Years='" + DateTime.Now.ToString("yyyyMM") + "'");
                //string sMonthReport = SQLHelper.ExecuteString("select count(*) from tb_ReportAnalysis where Area_ID='" + UserInfo.Rows[0][15].ToString().Trim() + "' and Years='" + DateTime.Now.ToString("yyyyMM") + "'");
                //dayNews = String.IsNullOrEmpty(dayNews) ? "15" : dayNews;
                //sMonthReport = String.IsNullOrEmpty(sMonthReport) ? "0" : sMonthReport;
                //if (news == "0" && int.Parse(dayNow) >= int.Parse(dayNews) && int.Parse(sMonthReport) == 0)
                //{
                //    SQLHelper.ExecuteNonQuery("insert into tb_Message(Area_ID,Years) values('" + UserInfo.Rows[0][15].ToString().Trim() + "','" + DateTime.Now.ToString("yyyyMM") + "')");
                //}
                //Response.Write("<script language=javascript>var i=screen.availWidth;var j=screen.availHeight;javascript:window.open('SysManage/index.htm','白马软件后台管理系统','toolbar=no,left=0,top=0,titlebar=no,location=no,status=yes,menubar=no,scrollbars=no,resizable=yes,scrolling=no,width='+i+',height='+j+'');window.opener=null;window.close();<" + "/script>");
                Response.Redirect("SysManage/index.htm");
                //}
            }
            else
            {
                string sContent = "用户" + sLogonID + "输入帐户时,密码或者帐号不正确！";
                string sIP = Page.Request.UserHostAddress;
                MyLog.InsertLog("1", "0", sContent, sIP);
                LogonID.Focus();
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('密码或帐号不正确,或者该用户权限被限制,请和系统管理员联系!');</script>");
                return;
            }
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('此系统未正确加载相关项,请与系统管理人员联系!');</script>");
            return;
        }
    }
}
